﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using hachiModel;

namespace Hachi.Controllers
{
    public class ServeyController : BaseController
    {
        public ServeyController()
        { 
        }

        public SV_Question QuestionGet()
        {
            DateTime td = DateTime.Now;
            return en.SV_Question.Where(d=>d.IsApproved==true && d.FromDate<=td && td<=d.ToDate).OrderBy(o=>o.SortOrder).FirstOrDefault();
        } 
        public SV_Question QuestionGet(int pQuestionID)
        {
            return en.SV_Question.Where(d => d.QuestionID == pQuestionID).FirstOrDefault();
        }
        public SV_QuestionDetail QuestionDetailGet(SV_Question pQuesstion, string Lang)
        {
            return pQuesstion.SV_QuestionDetail.Where(d=>d.Language==Lang).FirstOrDefault();
        }
        public List<SV_Answer> AnswerGet(SV_Question pQuestion)
        {
            return pQuestion.SV_Answer.OrderBy(o=>o.SortOrder).ToList();
        }
        public SV_AnswerDetail AnswerDetailGet(SV_Answer pAnswer, string Lang)
        {
            SV_AnswerDetail ad = pAnswer.SV_AnswerDetail.Where(d => d.Language == Lang).FirstOrDefault();
            if (ad==null)
                ad = pAnswer.SV_AnswerDetail.FirstOrDefault();
            if (ad==null)
                ad = new SV_AnswerDetail();
            return ad;
        }
        public bool AddServey(int AnswerID, string UserName, string NameServey, string OrtherAnswer)
        {
            try
            {
                SV_Servey s = new SV_Servey();
                s.AnswerID = AnswerID;
                s.UserName = UserName;
                s.NameServey = NameServey;
                s.OtherAnswer = OrtherAnswer;
                s.CreatedDate = DateTime.Now;
                en.AddToSV_Servey(s);
                en.SaveChanges();
                return true;
            }
            catch (Exception exx)
            {
                return false;
            }
            
        }
    }
}